class Trie {

    public static class Node {
        int count;
        Node[] child;
        public Node() {
            this.count = 0;
            this.child = new Node[26];
        }
    }

    private Node root;

    /** Initialize your data structure here. */
    public Trie() {
        this.root = new Node();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        operate(word, true, false);
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        return operate(word, false, true);
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        return operate(prefix, false, false);
    }

    private boolean operate(String word, boolean insert, boolean search) {
        Node curr = root;
        for (char ch : word.toCharArray()) {
            int targetIdx = ch - 'a';
            if (curr.child[targetIdx] == null) {
                if (insert) {
                    curr.child[targetIdx] = new Node();
                } else {
                    return false;
                }
            }
            curr = curr.child[targetIdx];
        }
        if (insert) {
            curr.count++;
        }
        if (search) {
            return curr.count > 0;
        }
        return true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */
